import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '@/store/permiss';
import Home from '../views/home.vue';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
// 新增
import Login from '../views/pages/login.vue'

const routes: RouteRecordRaw[] = [
        {
            path: '/',
            redirect: '/login', // 将根路径重定向到登录页面
        },
        {
            path: '/login',
            meta: {noAuth: true},//允许未登录访问
            component: Login,
        },

    {
        path: '/home',
        component: Home,
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: '系统首页',
                    permiss: '0',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/dashboard.vue'),
            },

            {
                path: '/announcement',
                name: 'announcement',
                meta: {
                    title: '公告管理',
                    permiss: '11',
                    // role: 'admin',//只有管理员有管理权限
                },
                component: () => import(/* webpackChunkName: "system-announcement" */ '../views/system/announcement.vue'),
            },
            {
                path: '/user',
                name: 'user',
                meta: {
                    title: '用户管理',
                    permiss: '12',
                    // role: 'admin',//只有管理员有管理权限
                },
                component: () => import(/* webpackChunkName: "system-announcement" */ '../views/system/user.vue'),
            },
            {
                path: '/class',
                name: 'class',
                meta: {
                    title: '班级管理',
                    permiss: '13',
                    // role: 'admin',//只有管理员有管理权限
                },
                component: () => import(/* webpackChunkName: "system-class" */ '../views/system/class.vue'),
            },
            {
                path: '/activity',
                name: 'activity',
                meta: {
                    title: '学生活动管理',
                    permiss: '14',
                    // role: 'admin',//只有管理员有管理权限
                },
                component: () => import(/* webpackChunkName: "system-activity" */ '../views/system/activity.vue'),
            },
            {
                path: '/attendance',
                name: 'attendance',
                meta: {
                    title: '学生考勤管理',
                    permiss: '15',
                    // role: 'admin',//只有管理员有管理权限
                },
                component: () => import(/* webpackChunkName: "system-menu" */ '../views/system/attendance.vue'),
            },
            {
                path: '/reward',
                name: 'reward',
                meta: {
                    title: '学生奖罚管理',
                    permiss: '21',
                    // role: 'teacher',//只有老师有管理权限
                },
                component: () => import(/* webpackChunkName: "system-reward" */ '../views/system/reward.vue'),
            },
            {
                path: '/comment',
                name: 'comment',
                meta: {
                    title: '学生评价管理',
                    permiss: '22',
                    // role: 'teacher',//只有老师有管理权限
                },
                component: () => import(/* webpackChunkName: "system-comment" */ '../views/system/comment.vue'),
            },
            {
                path: '/homework',
                name: 'homework',
                meta: {
                    title: '学生作业管理',
                    permiss: '23',

                },
                component: () => import(/* webpackChunkName: "system-menu" */ '../views/system/homework.vue'),
            },
            {
                path: '/grade',
                name: 'grade',
                meta: {
                    title: '学生成绩管理',
                    permiss: '24',
                },
                component: () => import(/* webpackChunkName: "system-menu" */ '../views/system/grade.vue'),
            },
            {
                path: '/chat',
                name: 'chat',
                meta: {
                    title: '活动评论管理',
                    permiss: '25',
                    // role: 'teacher',//只有老师有管理权限
                },
                component: () => import(/* webpackChunkName: "system-menu" */ '../views/system/chat.vue'),
            },


        ],
    },

    {
        path: '/register',
        meta: {
            title: '注册',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "register" */ '../views/pages/register.vue'),
    },
    {
        path: '/reset-pwd',
        meta: {
            title: '重置密码',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "reset-pwd" */ '../views/pages/reset-pwd.vue'),
    },
    {
        path: '/403',
        meta: {
            title: '没有权限',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/pages/403.vue'),
    },
    {
        path: '/404',
        meta: {
            title: '找不到页面',
            noAuth: true,
        },
        component: () => import(/* webpackChunkName: "404" */ '../views/pages/404.vue'),
    },
    { path: '/:path(.*)', redirect: '/404' },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    // const role = localStorage.getItem('vuems_name');// 获取当前用户的角色
    const role = localStorage.getItem('vuems_role'); // 修改：从 localStorage 中读取角色信息

    const permiss = usePermissStore();// 获取权限状态

    // 检查是否需要权限校验
    if (!role && to.meta.noAuth !== true) {
        // 如果没有角色信息且当前路由需要权限校验，则跳转到登录页面
        next('/login');
    } else if (role && typeof to.meta.permiss == 'string' && !permiss.key.includes(to.meta.permiss)) {
        // 如果有角色信息，但当前路由需要特定权限且用户没有该权限，则跳转到403页面
        next('/403');
    } else {
        // 如果用户有权限或路由不需要权限校验，则允许访问
        next();
    }
});

router.afterEach(() => {
    NProgress.done();
});

export default router;
